package com.mjh.zhujianju.service.set.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.google.common.collect.Lists;
import com.livew.framework.base.model.FileEO;
import com.livew.framework.base.service.FileService;
import com.livew.framework.base.service.impl.BaseServiceImpl;
import com.livew.framework.utils.XTListUtils;
import com.mjh.zhujianju.dao.set.PermissionMapper;
import com.mjh.zhujianju.enums.BusinessEnum;
import com.mjh.zhujianju.model.set.PermissionEO;
import com.mjh.zhujianju.service.set.PermissionService;
import com.mjh.zhujianju.service.set.PermissionUserService;
import com.mjh.zhujianju.vo.set.PermissionVO;

/**
 * @author 刘洋
 * @Description: 权限
 * @date 2018/8/11 11:32
 */
@Service
public class PermissionServiceImpl extends BaseServiceImpl<PermissionEO> implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    
    @Resource
    private PermissionUserService permissionUserService;

    @Autowired
    private FileService fileService;

    public PermissionServiceImpl(PermissionMapper permissionMapper) {
        super(permissionMapper);
    }

    @Override
    public List<PermissionVO> queryMenus(String code) {
        List<PermissionVO> list = Lists.newArrayList();

        List<PermissionEO> permissionList =  permissionMapper.queryMenus(code);
        if (CollectionUtils.isEmpty(permissionList)){
            return list;
        }

        //组装-权限菜单
        permissionList.stream().forEach(permissionEO -> {
            PermissionVO permissionVO = new PermissionVO();
            BeanUtils.copyProperties(permissionEO,permissionVO);
            list.add(permissionVO);
        });

        //图片
        List<Long> caseIds = permissionList.stream().map(policyVO -> policyVO.getId())
                .distinct()
                .collect(Collectors.toList());

        List<FileEO> fileList = fileService.getFileByIds(caseIds,PermissionEO.class.getName(),
                BusinessEnum.TYPE_PERMISSION.getCode());
        if (CollectionUtils.isEmpty(fileList)){
            return list;
        }

        //组装-图片
        list.stream().forEach(permissionVO -> {
            fileList.stream().forEach(fileEO -> {
                if (permissionVO.getId().equals(fileEO.getCaseId())){
                    permissionVO.setFileEO(fileEO);
                }
            });
        });
        return list;
    }

	@Override
	public List<PermissionVO> getAll(Long userId) {
		List<PermissionEO> list =	this.permissionMapper.selectAll();
		if(XTListUtils.isEmpty(list)) {
			return null;
		}
		List<PermissionVO> result = new ArrayList<>();
		List<Long> isCheckList = null;
		if(userId!=null) {
		isCheckList = this.permissionUserService.getPermissionByUserId(userId);	
		}
		for (PermissionEO permissionEO : list) {
			PermissionVO vo = new PermissionVO();
			BeanUtils.copyProperties(permissionEO, vo);
			if(!XTListUtils.isEmpty(isCheckList)) {
				if(isCheckList.contains(permissionEO.getId())) {//选中了
					vo.setIsCheck(Boolean.TRUE);
				}
			}
			result.add(vo);
		}
		return result;
	}

	@Override
	public List<PermissionEO> getAllByIds(List<Long> ids) {
		if(XTListUtils.isEmpty(ids)) {
			return null;
		}
		List<PermissionEO> list =	permissionMapper.selectByIdList(ids);
		return list;
	}
	
	
	
}
